package com.wyq.service.impl;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.wyq.bean.Company;
import com.wyq.dao.CompanyDao;
import com.wyq.service.CompanyService;

public class CompanyServiceImpl implements CompanyService {
	private CompanyDao companyDAO;
	
	public void setCompanyDAO(CompanyDao companyDAO) {
		this.companyDAO = companyDAO;
	}

	public int deleteCompanyById(String id) throws Exception {
		return companyDAO.deleteCompanyById(id);
	}

	public List getAllCompany(String type) throws Exception {
		return companyDAO.getAllCompany(type);
	}

	public List getCompanyMaxID() throws Exception {
		return companyDAO.getCompanyMaxID();
	}

	public void saveCompany(Company company) throws Exception {
		companyDAO.saveCompany(company);
	}

	public List companyListByName(String colName, String value,String show_id)
			throws Exception {
		return companyDAO.companyListByName(colName, value,show_id);
	}

	public List getPageCompany(String start, String limit,String type) throws Exception {
		return companyDAO.getPageCompany(start, limit,type);
	}

	public List getCompanyById(String id) throws Exception {
		return companyDAO.getCompanyById(id);
	}

	//生成xls
	public InputStream getInputStream(String showInfoId) throws Exception {
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("sheet1");
		sheet.setDefaultColumnWidth((short)15);
		
		HSSFRow row = sheet.createRow(0);
		
		HSSFCell cell = row.createCell((short) 0);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("编号");
		
		cell = row.createCell((short) 1);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("企业名称");
		
		cell = row.createCell((short) 2);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("简称");
		
		cell = row.createCell((short) 3);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("企业地址");
		
		cell = row.createCell((short) 4);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("总经理");
		
		cell = row.createCell((short) 5);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("总经理手机");
		
		cell = row.createCell((short) 6);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("法人代表");

		cell = row.createCell((short) 7);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("法人代表手机");
		
		cell = row.createCell((short) 8);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("企业电话");
		
		cell = row.createCell((short) 9);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("企业传真");
		
		cell = row.createCell((short) 10);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("企业邮箱");
		
		cell = row.createCell((short) 11);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("企业网址");
		
		cell = row.createCell((short) 12);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("企业资产类型");
		
		cell = row.createCell((short) 13);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("企业经营类型");
		
		cell = row.createCell((short) 14);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("参展条件");
		
		cell = row.createCell((short) 15);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("参展品牌");
		
		cell = row.createCell((short) 16);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("企业荣誉");
		
		cell = row.createCell((short) 17);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("主要产品");
		
		cell = row.createCell((short) 18);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("负责人");
		
		cell = row.createCell((short) 19);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("负责人电话");
		
		cell = row.createCell((short) 20);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("邮政编码");
		
		cell = row.createCell((short) 21);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("通信地址");
		
		cell = row.createCell((short) 22);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("注册时间");
		
		cell = row.createCell((short) 23);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("展会名称");
		
		List list = companyDAO.getAllCompany(showInfoId);
		
		for(int i=0;i<list.size();i++){
			Map company = (Map)list.get(i);
			
			row = sheet.createRow(i+1);
			
			cell = row.createCell((short) 0);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell.setCellValue(i+1);
			
			cell = row.createCell((short) 1);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(company.get("CompName") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(company.get("CompName").toString());
			}

			cell = row.createCell((short) 2);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(company.get("EasyName") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(company.get("EasyName").toString());
			}

			cell = row.createCell((short) 3);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(company.get("CompAddr") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(company.get("CompAddr").toString());
			}

			cell = row.createCell((short) 4);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(company.get("Manager") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(company.get("Manager").toString());
			}

			cell = row.createCell((short) 5);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(company.get("ManagerTel") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(company.get("ManagerTel").toString());
			}

			cell = row.createCell((short) 6);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(company.get("Fman") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(company.get("Fman").toString());
			}

			cell = row.createCell((short) 7);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(company.get("FmanTel") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(company.get("FmanTel").toString());
			}

			cell = row.createCell((short) 8);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(company.get("CompTel") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(company.get("CompTel").toString());
			}

			cell = row.createCell((short) 9);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(company.get("CompFax") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(company.get("CompFax").toString());
			}

			cell = row.createCell((short) 10);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(company.get("CompEmail") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(company.get("CompEmail").toString());
			}

			cell = row.createCell((short) 11);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(company.get("CompUrl") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(company.get("CompUrl").toString());
			}

			cell = row.createCell((short) 12);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(company.get("AssetType") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(company.get("AssetType").toString());
			}

			cell = row.createCell((short) 13);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(company.get("ManageType") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(company.get("ManageType").toString());
			}

			cell = row.createCell((short) 14);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(company.get("MustCredentials") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(company.get("MustCredentials").toString());
			}

			cell = row.createCell((short) 15);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(company.get("CompBrands") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(company.get("CompBrands").toString());
			}

			cell = row.createCell((short) 16);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(company.get("CompApprove") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(company.get("CompApprove").toString());
			}

			cell = row.createCell((short) 17);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(company.get("CompProduct") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(company.get("CompProduct").toString());
			}

			cell = row.createCell((short) 18);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(company.get("PostName") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(company.get("PostName").toString());
			}

			cell = row.createCell((short) 19);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(company.get("PostTel") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(company.get("PostTel").toString());
			}

			cell = row.createCell((short) 20);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(company.get("PostCode") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(company.get("PostCode").toString());
			}
			
			cell = row.createCell((short) 21);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(company.get("PostAddr") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(company.get("PostAddr").toString());
			}
			
			cell = row.createCell((short) 22);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(company.get("RegisterDate") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(company.get("RegisterDate").toString());
			}
			
			cell = row.createCell((short) 23);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(company.get("showName") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(company.get("showName").toString());
			}
		}
		
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		wb.write(os);
		byte[] content = os.toByteArray();
		InputStream is = new ByteArrayInputStream(content);
		
		return is;
		
	}



}
